package com.eam.handler;

import com.eam.data.EamMaintainPlan;
import com.eam.data.EamMaintainPlanFacs;
import org.codehaus.jettison.json.JSONObject;
import org.openbravo.client.kernel.BaseActionHandler;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBDal;
import java.util.List;
import java.util.Map;

/**
 * 待维修设备开始维修按钮
 * @author zt
 */
public class RepairHandler extends BaseActionHandler {
	@Override
	protected JSONObject execute(Map<String, Object> parameters, String content) {
		JSONObject jsonRep = new JSONObject();
		String meg = "";
		try {
			OBContext.setAdminMode();
			String jsonIds = new JSONObject(content).getString("ids");
			// 获取勾选的待维修设备清单记录
			String[] ids = jsonIds.split("#####");
			String status = "SERVICE";
			for (String id : ids) {
				EamMaintainPlan plan = OBDal.getInstance().get(EamMaintainPlan.class, id);
				plan.setStatus(status);
				OBDal.getInstance().save(plan);
				String sql = "select * from Eam_Maintain_Plan_Facs where Eam_Maintain_Plan_id = ? ";
				List<EamMaintainPlanFacs> lists = (List<EamMaintainPlanFacs>) OBDal.getInstance().getSession()
						.createSQLQuery(sql).addEntity(EamMaintainPlanFacs.ENTITY_NAME).setString(0, id).list();
				for (EamMaintainPlanFacs list : lists) {
					EamMaintainPlanFacs item = OBDal.getInstance().get(EamMaintainPlanFacs.class, list.getId());
					item.setStatus(status);
					OBDal.getInstance().save(item);
				}
			}
			if (meg.equalsIgnoreCase("")) {
				jsonRep.put("flag", true);
				jsonRep.put("message", "处理成功");
				OBDal.getInstance().commitAndClose();
			} else {
				jsonRep.put("flag", false);
				jsonRep.put("message", meg);
				OBDal.getInstance().rollbackAndClose();
			}
		} catch (Exception e) {
			OBDal.getInstance().rollbackAndClose();
			e.printStackTrace();
		} finally {
			OBContext.restorePreviousMode();
		}
		return jsonRep;
	}
}
